/**
 * Renders into a toolbar button.
 *
 * @author Lucian CIOROGA
 */
Button = new Class({
    /**
     * @param {String} id		The id of the button (should be unique).
     * @param {Object} options	An option map for this button.
     *
     * Options are:
     * 		command		The command that will be executed (using execCommand) when the button is pressed.
     * 		con 		A path to the file containing the icon of the button.
     * 		hover		A path to the file containing the icon that will be displayed when hovering over a button
     */
    initialize: function(id, options){
        this.id = id;
        this.options = options;
    },
    
    /**
     * Renders this button for a given editor.
     * @param {Editor} editor	The editor in which this button exists.
     */
    render: function(editor){
        return this.build(this, editor);
    },
    
    /**
     * Builds this button for a given editor.
     * @param {Button} self		Reference to itself.
     * @param {Editor} editor	The editor in which this button exists.
     */
    build: function(self, editor){
        return component = new Element('img', {
            'src': self.options.icon,
            'styles': {
                'cursor': 'pointer',
				'float': 'left'
            },
            'events': {
                'click': function(){
                    self.execute(editor, null);
                },
                'mouseenter': function(){
                    this.src = self.options.hover;
                },
                'mouseleave': function(){
                    this.src = self.options.icon;
                }
            }
        });
    },
    
    /**
     * @param {Editor} editor	The editor for which to execute the button's action.
     * @param {Object} value	The value for the <code>execCommand</code> function.
     */
    execute: function(editor, value){
        editor.restore();
        
        Debug.info("executing command " + this.options.command + " with value : " + value);
        
        try {
            editor.content.execCommand(this.options.command, false, value);
        } catch (e) {
            Debug.error("failed in executing " + this.options.command + ". Cause: " + e.message);
        }
        
        editor.focus();
    }
});
